package com.calenda.setevent.api;

import android.annotation.TargetApi;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.Build;
import android.view.ContextThemeWrapper;

import com.androidquery.AQuery;
import com.androidquery.callback.AjaxCallback;
import com.androidquery.callback.AjaxStatus;
import com.calenda.setevent.SetEventApplication;

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class BaseAjaxCallBack<T> extends AjaxCallback<T> {

	public synchronized static AQuery getInstance(Context ctx) {
		AQuery aQuery = new AQuery(ctx);
		return aQuery;
	}

	private ProgressDialog mProgressDialog;

	private boolean mShowDialogAble = true;

	public void setShowDialogAble(boolean condition) {
		mShowDialogAble = condition;
	}

	@Override
	public void async(Context context) {

		if (mProgressDialog == null) {
			mProgressDialog = new ProgressDialog(new ContextThemeWrapper(
					context, android.R.style.Theme_Holo_Light_Dialog));
			mProgressDialog.setCanceledOnTouchOutside(false);
			mProgressDialog.setCancelable(true);

			mProgressDialog.setMessage("Loading...");
		}

		if (mShowDialogAble)
			mProgressDialog.show();

		super.async(context);
	}

	@Override
	public void callback(String url, T object, AjaxStatus status) {
		// TODO Auto-generated method stub
		super.callback(url, object, status);
		mProgressDialog.dismiss();

	}

	public interface IAjaxCallbackMMK {
		public int START = 0;
		public int ERROR = -1;
		public int COMPLETE = 1;

		public <T> void handler(T t, int code);

	}

}
